package com.socool.manager;

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;

import java.net.MalformedURLException;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;
import android.util.Log;


public class MySocketIO extends SocketIO implements IOCallback{
	
	final static String LOG_TAG="test";
	
	private static String Socket_URL = "http://116.34.8.116:8000";
	
	

	Handler handler = new Handler();
	String receive_event = null;
	String receive_value = null;
	ArrayList<String> mArraylist = null;
	
	public MySocketIO(){
		Log.d(LOG_TAG, "[MySocketIO]MySocketIO()");
		try {
			connect(Socket_URL, this);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	

	

	@Override
	public void onMessage(JSONObject json, IOAcknowledge ack) {
		try {
		
			Log.d(LOG_TAG, "Server said: " + json.toString(2));
		} catch (JSONException e) {
			e.printStackTrace();
			Log.d(LOG_TAG, "Error:" + e.getMessage());
		}
	}

	@Override
	public void onMessage(String data, IOAcknowledge ack) {

		Log.d(LOG_TAG, "data: " + data);

	}

	@Override
	public void onError(SocketIOException socketIOException) {
		System.out.println("an Error occured");
		socketIOException.printStackTrace();
		Log.d(LOG_TAG, "Error:" + socketIOException.getMessage());

		handler.post(new Runnable() {

			public void run() {
				//
			}

		});
		
	}

	@Override
	public void onDisconnect() {
		System.out.println("Connection terminated.");
		Log.d(LOG_TAG, "Connection terminated.");
	}

	@Override
	public void onConnect() {
		System.out.println("Connection established");
		Log.d(LOG_TAG, "Connection established");
	
		handler.post(new Runnable() {

			public void run() {
				//
			}

		});

	}

	@Override
	public void on(String event, IOAcknowledge ack, Object... args) {
	receive_event = event;
	
	
	
		
		Log.d(LOG_TAG, "event: " + event);
	
		if (event.equals("test")) {

			
			receive_value = (String) args[0];
			Log.d(LOG_TAG, "args : " + receive_value);
				handler.post(new Runnable() {

					public void run() {
						//
						
						
					}

				});
		}else if(event.equals("AuthInfo")){
			Log.d(LOG_TAG, "args.length : " + args.length);
			
			JSONObject json = (JSONObject)args[0];
			Log.d(LOG_TAG, "SERVER Receive json : " + json.toString());
			/*for (int i = 0; i < json.length(); i++) {
			//json.get("id");
				Log.d(LOG_TAG, "args : " + receive_value);
			}*/

			
		}

	}
}